//
//  NSDictionary+JCSafetyGetter.m
//  JCPublicKit-Objc
//
//  Created by JasonLee on 2020/4/24.
//

#import "NSDictionary+JCSafetyGetter.h"

@implementation NSDictionary (JCSafetyGetter)

- (NSString * _Nullable)jc_stringForKey:(NSString * _Nullable)key {
    if (key == nil) {
        return nil;
    }
    
    NSObject *anyObject = [self objectForKey:key];
    
    if (anyObject == nil) {
        return nil;
    }
    
    if ([anyObject isKindOfClass:[NSString class]]) {
        return (NSString *)anyObject;
    } else if ([anyObject isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@", (NSNumber *)anyObject];
    }
    
    return nil;
}

- (int)jc_intForKey:(NSString * _Nullable)key {
    if (key == nil) {
        return 0;
    }
    
    NSObject *anyObject = [self objectForKey:key];
    if (anyObject == nil) {
        return 0;
    }
    
    if ([anyObject isKindOfClass:[NSNumber class]]) {
        return [(NSNumber *)anyObject intValue];
    } else if ([anyObject isKindOfClass:[NSString class]]) {
        return [(NSString *)anyObject intValue];
    }
    
    return 0;
}

- (NSInteger)jc_integerForKey:(NSString * _Nullable)key {
    return (NSInteger)[self jc_intForKey:key];
}

- (long long)jc_longLongForKey:(NSString * _Nullable)key {
    if (key == nil) {
        return 0;
    }
    
    NSObject *anyObject = [self objectForKey:key];
    if (anyObject == nil) {
        return 0;
    }
    
    if ([anyObject isKindOfClass:[NSNumber class]]) {
        return [(NSNumber *)anyObject longLongValue];
    } else if ([anyObject isKindOfClass:[NSString class]]) {
        return [(NSString *)anyObject longLongValue];
    }
    
    return 0;
}


- (double)jc_doubleForKey:(NSString * _Nullable)key {
    if (key == nil) {
        return 0;
    }
    
    NSObject *anyObject = [self objectForKey:key];
    if (anyObject == nil) {
        return 0;
    }
    
    if ([anyObject isKindOfClass:[NSNumber class]]) {
        return [(NSNumber *)anyObject doubleValue];
    } else if ([anyObject isKindOfClass:[NSString class]]) {
        return [(NSString *)anyObject doubleValue];
    }
    
    return 0;
}

- (BOOL)jc_boolForKey:(NSString * _Nullable)key {
    if (key == nil) {
        return NO;
    }
    
    NSObject *anyObject = [self objectForKey:key];
    
    if (anyObject == nil) {
        return NO;
    }
    if ([anyObject isKindOfClass:NSString.class]) {
        return [(NSString *)anyObject boolValue];
    }
    if ([anyObject isKindOfClass:NSNumber.class]) {
        return [(NSNumber *)anyObject boolValue];
    }
    
    return NO;
}

- (NSDictionary * _Nullable)jc_dictionaryForKey:(NSString * _Nullable)key {
    if (key == nil) {
        return nil;
    }
    
    NSObject *anyObject = [self objectForKey:key];
    
    if (anyObject == nil) {
        return nil;
    }
    if (![anyObject isKindOfClass:NSDictionary.class]) {
        return nil;
    }
    return (NSDictionary *)anyObject;
}

- (NSArray * _Nullable)jc_arrayForKey:(NSString * _Nullable)key {
    if (key == nil) {
        return nil;
    }
    
    NSObject *anyObject = [self objectForKey:key];
    
    if (anyObject == nil) {
        return nil;
    }
    if (![anyObject isKindOfClass:NSArray.class]) {
        return nil;
    }
    return (NSArray *)anyObject;
}

@end
